package com.pimee.web.sys;

import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.shiro.ShiroUtils;
import com.pimee.model.SysDictData;
import com.pimee.service.admin.ISysDictDataService;
import com.pimee.web.AbstractController;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping(value = "admin/sys/dict/data")
public class SysDictDataController extends AbstractController {
	private String prefix = "admin/sys/dict/data";

    @Autowired
    private ISysDictDataService sysDictDataService;
    
    @ApiOperation("查询数据字典列表")
    @PostMapping("/list")
    @RequiresPermissions("system:dict:list")
    @ResponseBody
    public Object pageList(@RequestParam Map<String, Object> params, ModelMap modelMap) {
		PageInfo<SysDictData> pageList = sysDictDataService.pageList(params);
		return setSuccessModelMap(modelMap, pageList);
	}
    
    @ApiOperation("进入新增数据字典页面")
    @GetMapping("/toAdd/{dictType}")
    public String toAdd(@PathVariable("dictType") String dictType, ModelMap modelMap) {
    	modelMap.put("dictType", dictType);
        return prefix + "/add";
    }

    @ApiOperation("保存新增数据字典信息")
    @RequiresPermissions("system:dict:add")
    @PostMapping("/add")
    @ResponseBody
    public Object addSave(@Validated SysDictData param, ModelMap modelMap) {
    	String loginName = ShiroUtils.getLoginName();
    	param.setCreateBy(loginName);
    	param.setUpdateBy(loginName);
        sysDictDataService.insertDictData(param);
        return setSuccessModelMap(modelMap);
    }

    @ApiOperation("进入修改数据字典页面")
    @GetMapping("/toEdit/{dictCode}")
    public String edit(@PathVariable("dictCode") Long dictCode, ModelMap modelMap) {
    	modelMap.put("dict", sysDictDataService.selectByKey(dictCode));
        return prefix + "/edit";
    }

    @ApiOperation("保存修改数据字典信息")
    @RequiresPermissions("system:dict:edit")
    @PostMapping("/edit")
    @ResponseBody
    public Object editSave(@Validated SysDictData param, ModelMap modelMap) {
    	param.setUpdateBy(ShiroUtils.getLoginName());
        sysDictDataService.updateDictData(param);
        return setSuccessModelMap(modelMap);
    }
    
    @ApiOperation("删除数据字典信息")
    @RequiresPermissions("system:dict:remove")
    @PostMapping("/remove/{dictCode}")
    @ResponseBody
    public Object remove(@PathVariable("dictCode") Long dictCode, ModelMap modelMap) {
    	sysDictDataService.deleteByKey(dictCode);
        return this.setSuccessModelMap(modelMap);
    }
}
